package com.agileai.portal.portlets.simple;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletModeException;
import javax.portlet.PortletPreferences;
import javax.portlet.ProcessAction;
import javax.portlet.ReadOnlyException;
import javax.portlet.RenderMode;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ValidatorException;

import com.agileai.hotweb.domain.FormSelect;
import com.agileai.portal.driver.GenericPotboyPortlet;
import com.agileai.portal.driver.common.PreferenceException;
import com.agileai.portal.driver.common.PreferencesHelper;
import com.agileai.portal.driver.common.PreferencesWrapper;
import com.agileai.util.StringUtil;
/**
 * generated by miscdp
 */
public class TimerPortlet extends GenericPotboyPortlet {
	private static final String NewLine = "\r\n";
	private String getDefaultJsHandler(){
		StringBuffer result = new StringBuffer();
		result.append("function(){").append(NewLine);
		result.append("    doAjaxRefreshPortalPage('');").append(NewLine);
		result.append("}").append(NewLine);
		return result.toString();
	}
	
	@RenderMode(name = "view")
	public void view(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {
		PortletPreferences preferences =  PreferencesHelper.getPublicPreference(request);
		String timerRangeList = preferences.getValue("timerRangeList", null);
		String defaultRange = preferences.getValue("defaultRange", null);
		String isVisiable = preferences.getValue("isVisiable", null);
		String jsHandler = preferences.getValue("jsHandler", null);

		boolean isSetting = StringUtil.isNotNullNotEmpty(timerRangeList) 
				&& StringUtil.isNotNullNotEmpty(defaultRange)
				&& StringUtil.isNotNullNotEmpty(isVisiable)
				&& StringUtil.isNotNullNotEmpty(jsHandler);
		
		request.setAttribute("timerRangeList", timerRangeList);
		request.setAttribute("defaultRange", defaultRange);
		request.setAttribute("isVisiable", isVisiable);
		request.setAttribute("jsHandler", jsHandler);
		request.setAttribute("isSetting", String.valueOf(isSetting));
		if (isSetting){
			String timerRangeListHtml = getTimerRangeListText(timerRangeList, defaultRange);
			request.setAttribute("timerRangeListHtml", timerRangeListHtml);
		}
		super.doView(request, response);
	}
	
	private String getTimerRangeListText(String timerRangeList,String sv){
		String result = null;
		String[] timeRanges = timerRangeList.split(",");
		FormSelect formSelect = new FormSelect();
		for (int i=0;i < timeRanges.length;i++){
			String value = timeRanges[i];
			formSelect.putValue(value, value);	
		}
		formSelect.setHasBlankValue(false);
		formSelect.addSelectedValue(sv);
		result = formSelect.getSyntax();
		return result;
	}
	
	@RenderMode(name = "edit")
	public void edit(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {
		PortletPreferences preferences = PreferencesHelper.getPublicPreference(request);
		String timerRangeList = preferences.getValue("timerRangeList", "3,5,10,20,30,40");
		String defaultRange = preferences.getValue("defaultRange", "5");
		String isVisiable = preferences.getValue("isVisiable", "Y");
		String jsHandler = preferences.getValue("jsHandler", getDefaultJsHandler());
		
		request.setAttribute("timerRangeList", timerRangeList);
		request.setAttribute("defaultRange", defaultRange);
		request.setAttribute("isVisiable", isVisiable);
		request.setAttribute("jsHandler", jsHandler);

		super.doEdit(request, response);
	}

	@ProcessAction(name = "saveConfig")
	public void saveConfig(ActionRequest request, ActionResponse response)
			throws ReadOnlyException, PortletModeException, ValidatorException,
			IOException, PreferenceException {
		String timerRangeList = request.getParameter("timerRangeList");
		String defaultRange = request.getParameter("defaultRange");
		String isVisiable = request.getParameter("isVisiable");
		String jsHandler = request.getParameter("jsHandler");

		PreferencesWrapper preferWapper = new PreferencesWrapper();		
		preferWapper.setValue("timerRangeList", timerRangeList);
		preferWapper.setValue("defaultRange", defaultRange);
		preferWapper.setValue("isVisiable", isVisiable);
		preferWapper.setValue("jsHandler", jsHandler);
		
		PreferencesHelper.savePublicPreferences(request, preferWapper.getPreferences());	
		response.setPortletMode(PortletMode.VIEW);
	}
}
